package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Model;

public class UpdatePanel extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	Model model;
	TopPanel parent;
	
	JButton updateButton;
	JTextField updateCountField;

	public UpdatePanel(Model model, TopPanel parent) {
		this.model = model;
		this.parent = parent;
		setLayout(new BorderLayout());
		updateButton = new JButton("Update");
		updateCountField = new JTextField(4);
		
		updateButton.addActionListener(this);
		
		add(updateButton, BorderLayout.WEST);
		add(updateCountField, BorderLayout.EAST);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String updateInput = updateCountField.getText();
		System.out.println(updateInput);
		int updateCount;
		try{
			updateCount = Integer.parseInt(updateInput);
		} catch(Exception e) {
			updateCount =1;;
		}
		for(int i=0; i<updateCount; i++){
			model.update();
			parent.addText(model.getStatusString());
		}
		
	}
	
}
